package com.globalwave.common;

import java.sql.Timestamp;
import java.sql.Types;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

public class U {
    public static Date currentDate() {
    	return new Date(System.currentTimeMillis()) ;
    }

    public static Timestamp currentTimestamp() {
    	return new Timestamp(System.currentTimeMillis()) ;
    }

    public static Date saleDate() {
    	return new Date(System.currentTimeMillis()) ;
    }

	public static String yyyyMMdd(Date date) {
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date);
	}

	public static String yyyyMMddHHmmss(Date date) {
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(date);
	}

	public static Long date2int(Date date) {
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return Long.valueOf(df.format(date));
	}

    public static Date int2Date(long iDate) throws Exception {
    	if (iDate < 100000000) {
    		return new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(iDate)) ;
    	} else {
    		return new SimpleDateFormat("yyyyMMddHHmmss").parse(String.valueOf(iDate)) ;
    	}
    }
	
    public static Date addMonths(Date date, int months) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date) ;
        calendar.add(GregorianCalendar.MONTH, months);
        return calendar.getTime();
    }
	
    public static Date addDates(Date date, int dates) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date) ;
        calendar.add(GregorianCalendar.MONTH, dates);
        return calendar.getTime();
    }

    public static Date lastDayOfMonth(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date) ;
        return new Date(calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
    }
    
	public static Object convert(String valueAsString, Integer type) throws Exception {
		if (type == null) {
			return valueAsString ;
		}
		
		switch (type) {
		case Types.INTEGER:			
			return Integer.valueOf(valueAsString);

		case Types.BIGINT:			
			return Long.valueOf(valueAsString);

		case Types.TIMESTAMP:			
			return new Timestamp(DateUtils.parseDate(valueAsString, new String[]{"yyyy-MM-dd hh:mm:ss"}).getTime());

		case Types.DOUBLE:			
			return Double.valueOf(valueAsString);

		case Types.DATE:			
			return DateUtils.parseDate(valueAsString, new String[]{"yyyy-MM-dd"});

		default:
			return valueAsString;
		}
	}
	
	public static Map<String, String> parseParams(final String paramsAsStr) {
		
		if (StringUtils.isEmpty(paramsAsStr)) {
			return null ;
		}

		Map<String, String> context = new HashMap<String, String>() ;
		
		if (!StringUtils.isEmpty(paramsAsStr)) {
			String[] paramsKeyValue = paramsAsStr.split("&") ;
			for (String keyValue:paramsKeyValue) {
				String[] params = keyValue.split("=") ;
			    context.put(params[0], params[1]) ;
			}
		}
		
		return context ;
	}
	
}
